package com.sp.scmdb.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.sp.scmdb.bean.AlarmBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.AlarmType;
import com.sp.scmdb.exception.ExcelException;
import com.sp.scmdb.service.AlarmService;
import com.sp.scmdb.service.ExportExcelService;

@Controller("alarmAction")
@Scope("prototype")
public class AlarmAction extends BaseAction<AlarmBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private AlarmService alarmService;
	private static Log log = LogFactory.getLog(AlarmAction.class);
	private ExportExcelService exportExcelService;
	private InputStream imagStream;

	public InputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	private InputStream excelStream;
	private String fileName;

	@Autowired
	public void setExportExcelService(ExportExcelService exportExcelService) {
		this.exportExcelService = exportExcelService;
	}

	/**
	 * @param alarmService
	 *            the alarmService to set
	 */
	@Autowired
	public void setAlarmService(AlarmService alarmService) {
		this.alarmService = alarmService;
	}

	@SuppressWarnings("unchecked")
	public String findAll() {
		try {
			AlarmBean alarmBean = null;

			if (StringUtils.isNotEmpty(pageName)) {
				alarmBean = (AlarmBean) session.get("alarmBean");
			} else {
				entity.setPageNo(1);
			}

			if (!StringUtils.isNotEmpty(entity.getAlarmType())) {
				messageSession("0", "请选择报警类型！");
				return SUCCESS;
			}

			if (!StringUtils.isNotEmpty(entity.getStartTime())) {
				messageSession("0", "请输入开始时间！");
				return SUCCESS;
			}

			if (!StringUtils.isNotEmpty(entity.getEndTime())) {
				messageSession("0", "请输入结束时间！");
				return SUCCESS;
			}

			// 获取报警类型
			List<AlarmType> ListAT = alarmService.findAllAlarmType();
			session.put("ListAlarmType", ListAT);
			setEntity(alarmService.findAll(entity, alarmBean));
			session.put("alarmBean", entity);

			return SUCCESS;
		} catch (Exception e) {
			return "fail";
		}
	}

	// 查找人员信息
	public String findEmp() {
		UserBean user = (UserBean) session.get("user");

		try {
			String empId = request.getParameter("id");
			AlarmBean ab = alarmService.findEmp(entity, empId, user);
			if (ab == null) {
				request.setAttribute("message", "人员不存在！");
			}
		} catch (Exception e) {
			log.error("finEmp fail" + e.getMessage());
			return "login";
		}
		return "editSucc";
	}

	public String getImag() {
		UserBean user = (UserBean) session.get("user");
		String empId = request.getParameter("id");
		AlarmBean ab = alarmService.findEmp(entity, empId, user);
		if (ab.getEmployeeBean() == null) {
			request.setAttribute("message", "人员不存在！");
		} else {
			try {
				imagStream = new ByteArrayInputStream(ab.getEmployeeBean().getPhoto());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "imag";
	}

	@SuppressWarnings("unchecked")
	public String jumpAlarm() {

		// 清楚内存中的数据
		session.remove("alarmBean");

		// 获取报警类型
		List<AlarmType> ListAT = alarmService.findAllAlarmType();
		if(CollectionUtils.isNotEmpty(ListAT)){
			for (AlarmType alarmType : ListAT) {
				if(alarmType.getId()==8){
					ListAT.remove(7);
					break;
				}
			}
		}
		session.put("ListAlarmType", ListAT);
		session.put("confirmType", CommonParamter.TypeCommon.getConfirmType());

		return SUCCESS;
	}

	public String alarmExcelReport() {
		try {
			Workbook wb = new HSSFWorkbook();
			AlarmBean alarmBean = (AlarmBean) session.get("alarmBean");
			String tableName = "报警信息报表";
			if (alarmBean != null) {
				wb = exportExcelService.createAlarmReportExcel(alarmBean, tableName);
			}
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			tableName = new String("报警信息报表".getBytes(), "ISO-8859-1");
			this.fileName = tableName + ".xls";
			wb.write(out);
			excelStream = new ByteArrayInputStream(out.toByteArray());
			out.flush();
			out.close();
			return "excelReport";

		} catch (ExcelException excel) {
			log.error("create dau report excel failed! reason:" + excel.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("alarm  report  excel failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public InputStream getImagStream() {
		return imagStream;
	}

	public void setImagStream(InputStream imagStream) {
		this.imagStream = imagStream;
	}
}
